﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XchangeStreamer.Performance.SDK;
using XchangeStreamer.Benchmark.Core;
using XchangeStreamer.Utilities;

namespace XchangeStreamer.Performance
{
    class Program
    {
        //private static Connector connector;
        private static PerformanceMonitor perfMonitor;

        static void Main(string[] args)
        {
            perfMonitor = new PerformanceMonitor(1, 10, new Action<CounterInfo[]>(Print),
                    new CounterInfo(PerfCategories.DotNetClrLocksAndThreads, PerfCounters.TotalNoOfContentions, "XchangeStreamer.Subscriber"),
                    new CounterInfo(PerfCategories.DotNetClrMemory, PerfCounters.NoOfBytesInAllHeaps, "XchangeStreamer.Subscriber"),
                    new CounterInfo(PerfCategories.DotNetClrMemory, PerfCounters.PercentOfTimeInGC, "XchangeStreamer.Subscriber"),
                    new CounterInfo(PerfCategories.Process, PerfCounters.PercentOfProcessorTime, "XchangeStreamer.Subscriber"),
                    new CounterInfo(PerfCategories.Process, PerfCounters.IoOtherBytesPerSec, "XchangeStreamer.Subscriber"),
                    new CounterInfo(PerfCategories.Process, PerfCounters.WorkingSet, "XchangeStreamer.Subscriber"));

            perfMonitor.Start();
            Console.ReadKey(true);
            //Console.Write("Please enter server info (IP/Port):");
            //string[] serverInfo = Console.ReadLine().Split('/');
            //connector = Connector.Connection;
            //connector.Connect(serverInfo[0], serverInfo[1]);

            //Single cpu;
            //Single memory;
            //uint threads;
            //Single network;

            //do
            //{
            //    connector.GetPerformanceCounters(out cpu, out memory, out threads, out network);
            //    Console.WriteLine("CPU: [{0}] | MEMORY: [{1}] | THREADS: [{2}] | NETWORK: [{3}]", cpu, memory, threads, network);
            //} 
            //while (Console.ReadKey(true).Key == ConsoleKey.C);

            //Console.ReadKey();
            //connector.Disconnect();
        }

        private static void Print(CounterInfo[] counters)
        {
            var categories = from counter in counters
                             group counter by counter.CategoryName;

            foreach (var category in categories)
            {
                Printer.PrintLine();
                Console.WriteLine("Category: {0} :-", category.Key);
                Console.WriteLine();
                Printer.PrintRow("Counter", "Last", "Average", "Minimum", "Maximum");

                foreach (var counter in category)
                {
                    Printer.PrintRow(counter.CounterName, counter.Last.ToString(), counter.Average.ToString(), counter.Minimum.ToString(), counter.Maximum.ToString());
                }
            }

            perfMonitor.Start();
        }
    }
}
